﻿using System.Collections.Generic;
using HCQ2_IBLL;
using HCQ2_Model;
using HCQ2_Model.VR.Params;
using HCQ2_Model.WeiXinApiModel.ParamModel;
using HCQ2_Model.WeiXinApiModel.ResultApiModel;
using System.Linq;
using HCQ2_Common;
using System.Text.RegularExpressions;

namespace HCQ2_BLL
{
    public partial class T_VRBoardADBLL: IT_VRBoardADBLL
    {
        /// <summary>
        ///  获取广告列表
        /// </summary>
        /// <param name="paran"></param>
        /// <returns></returns>
        public List<VRBoardADModel> GetInitTable(BoardQueryParam paran)
        {
            List<T_VRBoardAD> list = DBSession.IT_VRBoardADDAL.GetInitTable(paran);
            List<VRBoardADModel> model = new List<VRBoardADModel>();
            list.ForEach(s => { model.Add(s.toPOTO()); });
            return model;
        }
        /// <summary>
        ///  获取广告列表数量
        /// </summary>
        /// <param name="paran"></param>
        /// <returns></returns>
        public int GetInitCount(BoardQueryParam paran)
        {
            return DBSession.IT_VRBoardADDAL.GetInitCount(paran);
        }
        /// <summary>
        ///  获取广告
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<T_VRBoardAD> GetBoardByChart(BoardChartParam model)
        {
            return DBSession.IT_VRBoardADDAL.GetBoardByChart(model);
        }

        //********************************************头记接口*************************************************
        /// <summary>
        ///  获取随机检测单词数据
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<RandomWordResult> GetRandomData(RandomWordParam param)
        {
            T_DocumentInfo file = DBSession.IT_DocumentInfoDAL.Select(s => s.file_code.Equals(param.file_code) && s.file_status == 5).FirstOrDefault();
            if (file == null)
                return null;
            List<T_VRCustomPassWord> list = DBSession.IT_VRBoardADDAL.GetCustomPassWord(param, file.file_id);
            if (list == null || list.Count < param.num)
                return null;
            List<int> rand = RandomHelper.GetListInt(1, list.Count, param.num);
            rand.Sort();//默认升序排列
            List<RandomWordResult> result = new List<RandomWordResult>();
            int index = 0;
            rand.ForEach(s => 
            {
                index += 1;
                var item = list[s - 1];
                string word = item.word, zimu = string.Empty;
                bool isSuccess = false;
                string pattern = @"^[A-Za-z]+$";
                int serial;
                do 
                {
                    serial = RandomHelper.GetSingleInt(1, item.word.Length);
                    zimu = word.Substring(serial - 1, 1);
                    Match match = Regex.Match(zimu, pattern);
                    isSuccess = match.Success;
                } while (!isSuccess);
                RandomWordResult temp = new RandomWordResult
                {
                    file_id = item.file_id,
                    cp_id = item.cp_id,
                    order = index,
                    word = item.word,
                    gj_symbol = item.gj_symbol,
                    kk_symbol = item.kk_symbol,
                    serial = serial,
                    cp_type = item.cp_type,
                    china_mean = item.china_mean
                };
                List<WordResultDetail> options = new List<WordResultDetail>();
                //options.Add(new WordResultDetail { zimu = zimu, correct = true });
                List<string> words = new List<string>();
                List<WordResultDetail> opetionTemp = new List<WordResultDetail>();//用于临时存储其余错误3项
                words.Add(zimu);
                int ziLen = 0;
                do 
                {
                    string randStr = RandomHelper.GetSingleZiMu();
                    if (!words.Contains(randStr))
                    {
                        words.Add(randStr);
                        opetionTemp.Add(new WordResultDetail { zimu = randStr, correct = false });
                        ziLen++;
                    }
                } while (ziLen < 3);
                //处理备选项
                //1.获取4以内的任一随机数
                int randNum = RandomHelper.GetSingleInt(1, 5);//正确答案随机位置
                int tempIndex = 2;
                for(int i = 1; i <= 4; i++)
                {
                    if(randNum==i)
                        options.Add(new WordResultDetail { zimu = zimu, correct = true });
                    else
                    {
                        options.Add(opetionTemp[tempIndex]);
                        tempIndex--;
                    }
                }
                temp.options = options;
                result.Add(temp);
            });
            return result;
        }
        /// <summary>
        ///  获取已购买商品数量
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int GetPayGoodNum(PayGoodNumParam model, int user_id)
        {
            return DBSession.IT_VRBoardADDAL.GetPayGoodNum(model, user_id);
        }
    }
}
